package com.platform.comm.service.impl;

import com.platform.comm.repository.CommBatchInfoRepository;
import com.platform.comm.service.ICommBatchInfoBiz;
import com.platform.comm.entity.CommBatchInfoEntity;
import com.platform.comm.entity.QCommBatchInfoEntity;
import com.platform.enums.BatchJobStatus;
import com.platform.enums.BatchJobType;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 */
@Service
public class CommBatchInfoBizImpl extends CommonBizImpl<CommBatchInfoRepository, CommBatchInfoEntity, String> implements ICommBatchInfoBiz {

    private QCommBatchInfoEntity qDirBatchInfoEntity = QCommBatchInfoEntity.dirBatchInfoEntity;

    public CommBatchInfoBizImpl(CommBatchInfoRepository dirBatchInfoRepository) {
        super(dirBatchInfoRepository);
    }

    @Override
    public CommBatchInfoEntity createExportDirJob(CommBatchInfoEntity dirBatchInfoEntity) {
        return this.saveEntity(dirBatchInfoEntity);
    }

    @Override
    public List<CommBatchInfoEntity> queryBatchJobForRunning() {
        return this.queryAll(qDirBatchInfoEntity.jobStatus.eq(BatchJobStatus.STARTED.code).and(qDirBatchInfoEntity.jobType.eq(BatchJobType.IMPORT.code)));
    }

    @Override
    public void failedBatchJobForRunning(List<CommBatchInfoEntity> list) {
        list.stream().peek(entity -> entity.setJobStatus(BatchJobStatus.FAILED.code)).forEach(this::saveEntity);
    }
}
